package com.leyou.advice;

import com.leyou.myException.LyException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author jshstart
 * @creat 2020-12-08 21:42
 */
@Slf4j
@Aspect
@Component
public class CommonLogAdvice {
    @Around("within(@org.springframework.stereotype.Service *)|| within(com.baomidou.mybatisplus.extension.service.IService+)")
    public Object handleLogException(ProceedingJoinPoint jp) throws  Throwable{
        try {
            log.debug("{}方法准备调用，参数{}",jp.getSignature(), Arrays.toString(jp.getArgs()));
            long startTime = System.currentTimeMillis();
            Object result = jp.proceed();
            log.debug("{}方法调用成功，执行耗时{}",jp.getSignature(),System.currentTimeMillis()-startTime);
            return result;
        } catch (Throwable throwable) {
            log.error("{}方法调用失败，原因{}",jp.getSignature(),throwable.getMessage(),throwable);
            if (throwable instanceof LyException){
              throw  throwable;
            }else {
            throw new LyException(500,throwable);
            }
        }
    }
}
